package com.ruoyi.laiqianba.service.impl;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.mapper.AddressInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.ApplicationInfoMapper;
import com.ruoyi.db.domain.ApplicationInfo;
import com.ruoyi.laiqianba.service.IApplicationInfoService;

/**
 * 应用包管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-02
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class ApplicationInfoServiceImpl implements IApplicationInfoService {
    @Autowired
    private ApplicationInfoMapper applicationInfoMapper;
    @Autowired
    AddressInfoMapper addressInfoMapper;

    /**
     * 查询应用包管理
     *
     * @param id 应用包管理主键
     * @return 应用包管理
     */
    @Override
    public ApplicationInfo selectApplicationInfoById(Long id) {
        ApplicationInfo applicationInfo = applicationInfoMapper.selectApplicationInfoById(id);

//        if (applicationInfo != null) {
//            String outCityArrayJson = applicationInfo.getOutCityArray();
//            if (outCityArrayJson != null && !outCityArrayJson.isEmpty()) {
//                try {
//
//                    JSONArray jsonArray = JSON.parseArray(outCityArrayJson);
//                    List<String> cityCodes = jsonArray.toJavaList(String.class);
//                    List<Map<String, Object>> provinceInfos = addressInfoMapper.getProvince(cityCodes);
//                    Map<String, String> cityToTargetValueMap = new HashMap<>();
//                    for (Map<String, Object> provinceInfo : provinceInfos) {
//                        Object cityCodeObj = provinceInfo.get("provinceCode");
//                        Object targetValueObj = provinceInfo.get("city");
//
//                        if (cityCodeObj != null && targetValueObj != null) {
//                            cityToTargetValueMap.put(cityCodeObj.toString(), targetValueObj.toString());
//                        }
//                    }
//                    List<String> provinceNames = new ArrayList<>();
//                    for (String cityCode : cityCodes) {
//                        String provinceName = cityToTargetValueMap.get(cityCode);
//                        if (provinceName != null) {
//                            provinceNames.add(provinceName);
//                        } else {
//                            provinceNames.add(cityCode);
//                        }
//                    }
//                    String provinceNamesString = String.join(",", provinceNames);
//                    applicationInfo.setOutCityArray(provinceNamesString);
//
//                } catch (Exception e) {
//                    System.err.println("处理 outCityArray 失败: " + e.getMessage());
//                }
//            }
//        }

        return applicationInfo;
    }


    /**
     * 查询应用包管理列表
     *
     * @param applicationInfo 应用包管理
     * @return 应用包管理
     */
    @Override
    public List<ApplicationInfo> selectApplicationInfoList(ApplicationInfo applicationInfo) {
        return applicationInfoMapper.selectApplicationInfoList(applicationInfo);
    }

    /**
     * 查询应用包管理列表
     *
     * @param applicationInfo 应用包管理
     * @return 应用包管理
     */
    @Override
    public List<Map<String, Object>> selectApplicationInfoMapList(ApplicationInfo applicationInfo) {
        List<Map<String, Object>> maps = applicationInfoMapper.selectApplicationInfoMapList(applicationInfo);
        try {
            Set<String> allCityCodes = new HashSet<>();
            for (Map<String, Object> map : maps) {
                Object outCityArrayObj = map.get("outCityArray");
                if (outCityArrayObj instanceof String) {
                    String jsonString = (String) outCityArrayObj;
                    JSONArray jsonArray = JSON.parseArray(jsonString);
                    List<String> cityCodes = jsonArray.toJavaList(String.class);
                    allCityCodes.addAll(cityCodes);
                }
            }
            List<Map<String, Object>> provinceInfos = addressInfoMapper.getProvince(new ArrayList<>(allCityCodes));
            Map<String, String> cityToTargetValueMap = new HashMap<>();
            for (Map<String, Object> provinceInfo : provinceInfos) {
                Object cityCodeObj = provinceInfo.get("provinceCode");
                Object targetValueObj = provinceInfo.get("city");

                if (targetValueObj != null) {
                    cityToTargetValueMap.put(cityCodeObj.toString(), targetValueObj.toString());
                }
            }
            for (Map<String, Object> map : maps) {
                Object outCityArrayObj = map.get("outCityArray");
                if (outCityArrayObj instanceof String) {
                    String jsonString = (String) outCityArrayObj;
                    JSONArray jsonArray = JSON.parseArray(jsonString);
                    List<String> cityCodes = jsonArray.toJavaList(String.class);
                    List<String> targetValues = new ArrayList<>();
                    for (String cityCode : cityCodes) {
                        String targetValue = cityToTargetValueMap.get(cityCode);
                        if (targetValue != null) {
                            targetValues.add(targetValue);
                        } else {
                            targetValues.add(cityCode);
                        }
                    }
                    String provinceNamesString = String.join(",", targetValues);
                    map.put("outCityArray", provinceNamesString);
                }
            }
        } catch (Exception e) {
            System.err.println("处理 outCityArray 失败: " + e.getMessage());
        }
            return maps;
        }

        /**
         * 新增应用包管理
         *
         * @param applicationInfo 应用包管理
         * @return 结果
         */
        @Override
        public int insertApplicationInfo (ApplicationInfo applicationInfo)
        {
            applicationInfo.setCreateTime(DateUtils.getNowDate());
            return applicationInfoMapper.insertApplicationInfo(applicationInfo);
        }

        /**
         * 修改应用包管理
         *
         * @param applicationInfo 应用包管理
         * @return 结果
         */
        @Override
        public int updateApplicationInfo (ApplicationInfo applicationInfo)
        {
            applicationInfo.setUpdateTime(DateUtils.getNowDate());
            return applicationInfoMapper.updateApplicationInfo(applicationInfo);
        }

        /**
         * 批量删除应用包管理
         *
         * @param ids 需要删除的应用包管理主键
         * @return 结果
         */
        @Override
        public int deleteApplicationInfoByIds (Long[]ids)
        {
            return applicationInfoMapper.deleteApplicationInfoByIds(ids);
        }

        /**
         * 删除应用包管理信息
         *
         * @param id 应用包管理主键
         * @return 结果
         */
        @Override
        public int deleteApplicationInfoById (Long id)
        {
            return applicationInfoMapper.deleteApplicationInfoById(id);
        }

        @Override
        public List<Map<String, String>> getAppInfoMapList () {
            return applicationInfoMapper.getAppInfoMapList();
        }

    }
